﻿using System;
using System.Windows.Forms;
using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using RescueApp.Data;
using RescueApp.Models;

namespace RescueApp.UI
{
    public partial class frmUpdateDispatch : Form
    {
        private RescueContext db;
        private RescueDispatch dispatch;

        public frmUpdateDispatch()
        {
            InitializeComponent();
        }

        public frmUpdateDispatch(RescueContext db, RescueDispatch dispatch)
        {
            InitializeComponent();
            this.db = db;
            this.dispatch = dispatch;
        }

        private void frmUpdateDispatch_Load(object sender, EventArgs e)
        {
            txt_rescueID.Text = dispatch.RescueID.ToString();
            txt_driver.Text = dispatch.DispatchDriver;
            txt_vehicleLicense.Text = dispatch.DispatchVehicleLicense;
            dtp_arrivedTime.Value = dispatch.ArrivalTime;
            txt_cause.Text = dispatch.MalfunctionCause;
            txt_cost.Text =  dispatch.RescueCost.ToString();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            // 获取输入值

            int rescueID;
            string driver = txt_driver.Text.Trim();
            string vehicleLicense = txt_vehicleLicense.Text.Trim();
            DateTime arrivedTime = dtp_arrivedTime.Value;
            string cause = txt_cause.Text.Trim();
            float cost;

            // 验证所有字段是否已填写
            if (string.IsNullOrEmpty(txt_rescueID.Text) ||
                string.IsNullOrEmpty(driver) ||
                string.IsNullOrEmpty(vehicleLicense) ||
                string.IsNullOrEmpty(cause) ||
                string.IsNullOrEmpty(txt_cost.Text))
            {
                MessageBox.Show("请填写完整信息！");
                return;
            }

            // 验证救援申请ID是否为整数
            if (!int.TryParse(txt_rescueID.Text, out rescueID))
            {
                MessageBox.Show("救援申请ID必须为整数！");
                return;
            }

            // 验证救援费用是否为数字
            if (!float.TryParse(txt_cost.Text, out cost))
            {
                MessageBox.Show("救援费用必须为数字！");
                return;
            }

            dispatch.DispatchDriver = driver;
            dispatch.DispatchVehicleLicense = vehicleLicense;
            dispatch.ArrivalTime = arrivedTime;
            dispatch.RescueID = rescueID;
            dispatch.MalfunctionCause = cause;
            dispatch.RescueCost = cost;

            string sql = @"UPDATE [dbo].[RescueDispatch] SET 
                RescueID = @RescueID,
                DispatchDriver = @DispatchDriver,
                DispatchVehicleLicense = @DispatchVehicleLicense,
                ArrivalTime = @ArrivalTime,
                MalfunctionCause = @MalfunctionCause,
                RescueCost = @RescueCost
            WHERE DispatchID = @DispatchID";
            
            IDictionary<string, object> dict = new Dictionary<string, object>
            {
                { "@DispatchID", dispatch.DispatchID },
                { "@RescueID", rescueID },
                { "@DispatchDriver", driver },
                { "@DispatchVehicleLicense", vehicleLicense },
                { "@ArrivalTime", arrivedTime },
                { "@MalfunctionCause", cause },
                { "@RescueCost", cost }
            };

            SqlParameter[] parameters = dict.ToSqlParameters();
            int affected = db.ExecuteSql(sql, parameters);

            if (affected > 0)
            {
                MessageBox.Show("派遣记录更新成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("派遣记录更新失败！");
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}